import java.text.DecimalFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.zone.ZoneRules;

public class TelephoneBillCalculator {

    private static final int SECONDS_PER_MINUTE = 60;
    private static final int LIM1 = 20;
    private static final int LIM2 = 1800;
    private static final double PRICE1 = 0.05;
    private static final double PRICE2 = 0.1;

    private final DecimalFormat df = new DecimalFormat("#.##"); // 保留两位小数的 DecimalFormat 对象
    public  boolean isDaylightTime(final LocalDateTime localDateTime, final ZoneId zoneId) {
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
        ZoneRules rules = zonedDateTime.getZone().getRules();
        return rules.isDaylightSavings(zonedDateTime.toInstant());
    }
    public ZonedDateTime transTime(final String dateTimeString, final ZoneId zoneId) {

        // 解析时间字符串为 LocalDateTime 对象
        LocalDateTime localDateTime = LocalDateTime.parse(dateTimeString, DateTimeFormatter.ISO_LOCAL_DATE_TIME);

        // 将 LocalDateTime 对象转换为目标时区的 ZonedDateTime 对象
        ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
        return zonedDateTime;
    }
    public long calculateTimeSpan(final ZonedDateTime startTime, final ZonedDateTime endTime) {
        Duration duration = Duration.between(startTime, endTime);
        return duration.getSeconds();
    }

    public double getBill(final long second) {
        long minute;
        if (second > 0) {
            minute = (second / SECONDS_PER_MINUTE);
        } else if (second == 0) {
            return 0;
        } else {
            return Double.NaN;
        }

        if (second % SECONDS_PER_MINUTE != 0) {
            minute++;
        }

        if (0 < minute && minute <= LIM1) {
            return Double.parseDouble(df.format(minute * PRICE1)); // 格式化并返回值
        } else if (LIM1 < minute && minute <= LIM2) {
            return Double.parseDouble(df.format(PRICE1 * LIM1 + (minute - LIM1) * PRICE2)); // 格式化并返回值
        } else {
            return Double.NaN;
        }
    }
}
